package com.cangsan.erhai.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cangsan.erhai.common.PageBean;
import com.cangsan.erhai.common.R;
import com.cangsan.erhai.entity.TabFootball;
import com.cangsan.erhai.service.TabFootballService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/football")
public class TabFootballController {

    @Value("${upload.baseDir}")
    private  String uploadBasedir;
    @Autowired
    private TabFootballService tabFootballService;

    @PostMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        QueryWrapper<TabFootball> queryWrapper = new QueryWrapper<>();
        Page<TabFootball> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        Page<TabFootball> pageResult = tabFootballService.page(page,queryWrapper);
        Map<String,Object> result = new HashMap<>();
        List<TabFootball> records = pageResult.getRecords();
        result.put("dataList",records);
        result.put("total",pageResult.getTotal());
        return R.ok(result);
    }

    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody TabFootball tabFootball){
        tabFootballService.saveOrUpdate(tabFootball);
        return R.ok();
    }

    @GetMapping("/getListByType/{footballName}")
    public R getListByType(@PathVariable String footballName){
        QueryWrapper<TabFootball> queryWrapper = new QueryWrapper();
        queryWrapper.like("football_name",footballName);
        return R.ok(tabFootballService.list(queryWrapper));
    }


    @RequestMapping("/uploadInco")
    public Map<String,Object> uploadImage(MultipartFile file) throws IOException {
        Map<String,Object> resultMap = new HashMap<>();
        if(!file.isEmpty()){
            String originalFilename = file.getOriginalFilename();
            String suffinxName = originalFilename.substring(originalFilename.lastIndexOf("."));
            String uid = UUID.randomUUID().toString().replaceAll("-", "");
            String newFileName =uid+ DateUtil.format(new Date(),"yyyyMMdd")+suffinxName;
            FileUtils.copyInputStreamToFile(file.getInputStream(),new File(uploadBasedir+"/football",newFileName));
            resultMap.put("code",0);
            resultMap.put("msg","上传成");
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("title",newFileName);
            dataMap.put("src","files/football/"+newFileName);
            resultMap.put("data",dataMap);
        }
        return resultMap;
    }

    @GetMapping("/{id}")
    public R delete(@PathVariable Long id) {
        tabFootballService.removeById(id);
        return R.ok("删除成功");
    }
}
